package com.msl.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import com.msl.MslApp;
import com.msl.R;
import com.msl.adapter.MyFragmentPagerAdapter;
import com.msl.base.BaseFragmentActivity;
import com.msl.bean.AbDcodeBean;
import com.msl.bean.AbfBean;
import com.msl.dialog.ChooseDialog1;
import com.msl.fragment.Fragment130SCRK1;
import com.msl.fragment.Fragment130SCRK2;
import com.msl.tool.ToastUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SCRK130Activity1 extends BaseFragmentActivity {
    private String tag="SCRK130Activity1";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.ll1)
    LinearLayout ll1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.ll2)
    LinearLayout ll2;
    @Bind(R.id.ll)
    LinearLayout ll;
    @Bind(R.id.viewpager)
    ViewPager viewpager;
    @Bind(R.id.tv_f)
    TextView tvF;
    private int mIndex = 0;
    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    private FragmentTransaction ft;
    private List<TextView> mtvs = new ArrayList<TextView>();
    private List<LinearLayout> lls = new ArrayList<LinearLayout>();
    Fragment130SCRK1 of1 = null;
    Fragment130SCRK2 of2 = null;
    private AbDcodeBean abDcodeBean = new AbDcodeBean();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrk1);
        ButterKnife.bind(this);
        tvTitle.setText("生产入库");
        abDcodeBean = (AbDcodeBean)getIntent().getSerializableExtra(MslApp.codedea);
        initData();
    }

    private void initData() {
        mtvs.add(tv1);
        mtvs.add(tv2);
        lls.add(ll1);
        lls.add(ll2);

        Bundle bundle = new Bundle();
        bundle.putSerializable(MslApp.codedea, abDcodeBean);

        of2 = new Fragment130SCRK2();
        of2.setArguments(bundle);
        mFragments.add(of2);
        of1 = new Fragment130SCRK1();
        of1.setArguments(bundle);
        mFragments.add(of1);

        viewpager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        viewpager.setCurrentItem(mIndex);
        viewpager.setOffscreenPageLimit(2);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                for (int i = 0; i < mtvs.size(); i++) {
                    mtvs.get(i).setTextColor(Color.parseColor("#333333"));
                    lls.get(i).setBackgroundColor(Color.parseColor("#ffffff"));
                }
                mIndex = arg0;
                mtvs.get(mIndex).setTextColor(Color.parseColor("#22a5ff"));
                lls.get(mIndex).setBackgroundResource(R.mipmap.btn_allorder);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
        });
    }


    private void setIndex(int i) {
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        hideFragments();
        ft.show(mFragments.get(0));
        ft.commit();
    }


    private void hideFragments() {
        for (int i = 0; i < mFragments.size(); i++) {
            ft.hide(mFragments.get(i));
        }
    }


    @OnClick({R.id.iv_title_left, R.id.ll1, R.id.ll2,R.id.tv_f})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.ll1:
                viewpager.setCurrentItem(0);
                break;
            case R.id.ll2:
                viewpager.setCurrentItem(1);
                break;
            case R.id.tv_f:
                if(MslApp.listcode.size()!=0)
                {
                    ToastUtils.showSingleToast(context,"条码正在上传,请稍后进行操作");
                    return;
                }
                tvF.setEnabled(false);
                ChooseDialog1 nDialog = new ChooseDialog1(this, R.style.Dialog1, new ChooseDialog1.PriorityListener() {
                    @Override
                    public void setActivityText() {
                        waitDialog.show();
                        updatecode(MslApp.url+"?action=SubmitBill&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                                +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                                +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                                +"&billcode="+abDcodeBean.getData().get(0).getBillcode()
                                +"&billType="+"130"
                        );
                    }
                });
                WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                nDialog.show();
                break;
        }
    }

    void updatecode(String url)
    {
        try {
            client = new OkHttpClient.Builder()
                    .retryOnConnectionFailure(false)
                    .connectTimeout(90, TimeUnit.SECONDS)//设置连接超时时间
                    .readTimeout(90, TimeUnit.SECONDS)//设置读取超时时间
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                    ToastUtils.showSingleToast(SCRK130Activity1.this,"上传数据超时");
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.isSuccessful()){//回调的方法执行在子线程。
                        result = response.body().string();
                        Log.e(tag,"result="+result);
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(0);
                    }
                    else
                    {
                        waitDialog.dismiss();
                    }
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
            waitDialog.dismiss();
        }
    }

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0:
                    AbfBean ab1 = gson.fromJson(result, AbfBean.class);
                    if(ab1.getErrcode()==0)
                    {
                          SCRK130Activity1.this.finish();
                    }
                    else
                    {
                        ToastUtils.showSingleToast(SCRK130Activity1.this,ab1.getErrmsg());
                    }
                    break;
                default:
                    break;
            }
        }
    };
}


